<?php
    class Pages_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
            $this->load->database();
        }
        /**
         * function insert
        */
        function insert_record_into_table($record,$table)
        {
            $this->db->insert($table,$record);    
        }
        
        function get_product_category_by_id($id)
        {
            $this->db->select('*');
            $this->db->from('tblproduct_category');
            $this->db->where('id',$id);
            $this->db->where('isdelete',0);
            $this->db->where('ispublish',1);
            $query=$this->db->get();
            return $query->row_array();
        }
        
        function get_news_category_by_id($id)
        {
            $this->db->select('*');
            $this->db->from('tblnews_category');
            $this->db->where('id',$id);
            $this->db->where('isdelete',0);
            $this->db->where('ispublish',1);
            $query=$this->db->get();
            return $query->row_array();
        }
        
        function get_product_category_by_parentid($select,$parentid)
        {
            $this->db->select($select);
            $this->db->from('tblproduct_category');
            $this->db->where('parentid',$parentid);
            $this->db->where('isdelete',0);
            $this->db->where('ispublish',1);
            $this->db->order_by('sort asc,id asc');
            $query=$this->db->get();
            return $query->result_array();
        }
        
        function get_product_by_categoryid($categoryid=null)
        {
            $this->db->select('*');
            $this->db->from('tblproduct');
            if(isset($categoryid))
            {
                $this->db->where('categoryid',$categoryid);
            }
            $this->db->where('ispublish',1);
            $this->db->order_by('sort asc,id desc');
            $query=$this->db->get();
            return $query->result_array();
        }
        
        function get_product_best_seller($limit)
        {
            $this->db->distinct();
            $this->db->select('*');
            $this->db->from('tblproduct');
            $this->db->where('ispublish',1);
            $this->db->order_by('sold desc,id desc');
            $this->db->limit($limit,0);
            $query = $this->db->get();
            return $query->result_array();
        }
        
        function get_product_newest($limit)
        {
            $this->db->distinct();
            $this->db->select('*');
            $this->db->from('tblproduct');
            $this->db->where('ispublish',1);
            $this->db->order_by('id desc');
            $this->db->limit($limit,0);
            $query = $this->db->get();
            return $query->result_array();
        }
        
        function get_introduction_company()
        {
            $this->db->select('introduction');
            $this->db->from('tblinfo_company');
            $this->db->where('id',1);
            $query = $this->db->get();
            return $query->row_array();
        }
        
        function get_product_by_id($id)
        {
            $this->db->select('*');
            $this->db->from('tblproduct');
            $this->db->where('ispublish',1);
            $this->db->where('id',$id);
            $query = $this->db->get();
            return $query->row_array();
        }
        
        function get_news_by_id($id)
        {
            $this->db->select('*');
            $this->db->from('tblnews');
            $this->db->where('ispublish',1);
            $this->db->where('id',$id);
            $query = $this->db->get();
            return $query->row_array();
        }
        
        function insert_data_to_tblcontact($record)
        {
            $this->db->insert('tblcontact',$record);   
        }
        
        function get_news_by_categoryid($categoryid=null)
        {
            $this->db->select('*');
            $this->db->from('tblnews');
            if(isset($categoryid))
            {
                $this->db->where('categoryid',$categoryid);
            }
            $this->db->where('ispublish',1);
            $this->db->order_by('sort asc,id desc');
            $query=$this->db->get();
            return $query->result_array();
        }
        
        function get_news_hot($limit)
        {
            $this->db->distinct();
            $this->db->select('*');
            $this->db->from('tblnews');
            $this->db->where('ispublish',1);
            $this->db->order_by('id desc');
            $this->db->limit($limit,0);
            $query = $this->db->get();
            return $query->result_array();
        }
        
        //insert for tblnewsrss
        function insert_array_news($arr_news=null)
        {
            foreach($arr_news as $record)
            {
                $this->db->select('title');
                $this->db->from('tblnewsrss');
                $this->db->where('title',$record['title']);
                $this->db->limit(1,0);
                $query = $this->db->get();
                
                //formate date
                //note: time slow 5h
                $this->load->helper('date');
                $datestring="%d-%m-%Y %h:%i:%s %A";
                $time=time();
                $format_time=mdate($datestring,$time);
                
                //create alias 
                $this->load->helper('common_function');
                
                if($query->num_rows==0)
                {
                    $row = array(
                            'title'=>$record['title'],
                            'summary'=> $record['description'],
                            'content'=>null,
                            'author'=>$record['author'],
                            'pubdate'=>$record['pubDate'],
                            'modifydate'=>$format_time,
                            'sort'=>1,
                            'ispublic'=>1,
                            'meta_keywords'=>null,
                            'meta_description'=>null,
                            'alias'=>standardize_url(trim($record['title']))
                    );
                    $this->db->insert('tblnewsrss',$row);
                }
            }
        }
    }
?>